<?php
/**
 * Created by PhpStorm.
 * User: ycq
 * Date: 18-12-9
 * Time: 下午10:24
 */
function get_soved($user_name)
{
    $curlobj = curl_init();            // 初始化
    curl_setopt($curlobj, CURLOPT_URL, "http://acm.hdu.edu.cn/userstatus.php?user=" . $user_name);        // 设置访问网页的URL
    curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);           // 执行之后不直接打印出来
    $output = curl_exec($curlobj);  // 执行
    curl_close($curlobj);          // 关闭cURL
//echo $output;
    $reg_tag_tr = '/<tr><td>Problems Solved<\/td><td align=center>\d+/';
//echo $reg_tag_tr;
    $res = [];
    preg_match_all($reg_tag_tr, $output, $res);
//var_dump($res);
    return isset($res[0][0])?substr($res[0][0], 45):false;
}

require_once 'config.php';
require_once 'time_record.php';
$db = mysqli_connect($db_addr, $db_user, $db_pass, $db_name, $db_prot);
!mysqli_connect_error() or die("连接失败！！");
$sql = "select * from data order BY slove DESC ";
$result = $db->query($sql);
$file=fopen('time','w+');
while(!feof($file)) {
    $date=date(fgets($file));
}
echo "榜单1分钟一次刷新<br/>";
echo '<table border="1"><tr><td>排名</td><td>ID</td><td>昵称</td><td>用户名</td><td>解决题数</td></tr>';
$i=1;
$flag=update();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    if($flag){
        $slove=get_soved($row['username']);
        if($slove){
            $update='update data set slove='.$slove.' where id='.$row['id'];
            $db->query($update);
        }
    }

    echo "<tr>" .
        "<td>{$i}</td>".
        "<td> {$row['id']}</td> " .
        "<td>{$row['name']} </td> " .
        "<td>{$row['username']} </td> " .
        "<td>{$row['slove']} </td> " .
        "</tr>";
    $i++;
}
?>
<a href="add.html">添加账号</a>
